#! /usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import shutil
import time
import sys
import tarfile

# make and goto dir
def makedir(mdir):
	# make build dir and go to
	if os.path.isdir(mdir):
		shutil.rmtree(mdir)
		os.mkdir(mdir)
	else:
		os.mkdir(mdir)
	os.chdir(mdir)


def buildsystem(values=[]):
	cmd = 'cmake-3.9.0-rc5-Linux-x86_64/bin/cmake ../ '
	for val in values:
		cmd = cmd + '-D' + val

	print(os.system(cmd))
	cmdMake = 'make'
	print(os.system(cmdMake))


def downloadcmake():
	downloadcmake = 'wget https://cmake.org/files/v3.9/cmake-3.9.0-rc5-Linux-x86_64.tar.gz'
	os.system(downloadcmake)


def untar(fname, dirs):
	print ('will install cmake')
	t = tarfile.open(fname)
	t.extractall(path = dirs)



if __name__ == '__main__':
	mkDir = 'build'
	makedir(mkDir)

	untar("../tools/cmake-3.9.0-rc5-Linux-x86_64.tar.gz", ".")

	vallist = ['buildsw=OFF']
	if len(sys.argv) == 2:
		if sys.argv[1] == 'mipi':
			vallist = ['buildsw=ON']
			print ('Start build mipi platform...................')

	time.sleep(1)
	buildsystem(vallist)

